<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sanpham_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library("unicode");
	}
	public function countSearch($tenUrl)
	{
		$str = $this->unicode->change($tenUrl);
		$query = $this->db->query("SELECT sanpham.id 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE LOWER(sanpham.tenUrl) REGEXP ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1",array($str));
		return ceil($query->num_rows()/9);
	}
	public function getSearch($tenUrl,$trang)
	{
		$batdau = (($trang-1)*9)-1;
		if($trang==1) $batdau = 0;
		$str = $this->unicode->change($tenUrl);
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE LOWER(sanpham.tenUrl) REGEXP ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu 
		LIMIT ".$batdau.",1",array($str));
		return $query->result();
	}
	public function getSPTrangChu()
	{
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu 
		LIMIT 0,9");
		return $query->result();
	}
	public function countAllSP()
	{
		$query = $this->db->query("SELECT sanpham.id 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1");
		return ceil($query->num_rows()/9);
	}
	public function getAllSP($trang)
	{
		$batdau = (($trang-1)*9)-1;
		if($trang==1) $batdau = 0;
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu 
		LIMIT ".$batdau.",9");
		return $query->result();
	}
	public function countSPByTenLP($tenLP)
	{
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE loaiphong.tenUrl = ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1",array($tenLP)); 
		return ceil($query->num_rows()/9);
	}
	public function getSPByTenLP($tenLP,$trang)
	{
		$batdau = (($trang-1)*9)-1;
		if($trang==1) $batdau = 0;
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE loaiphong.tenUrl = ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu 
		LIMIT ".$batdau.",9",array($tenLP));
		return $query->result();
	}
	public function countSPByTenL($tenL)
	{
		$query = $this->db->query("SELECT sanpham.id 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE loai.tenUrl = ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu",array($tenL));
		return ceil($query->num_rows()/9);
	}
	public function getSPByTenL($tenL,$trang)
	{
		$batdau = (($trang-1)*9)-1;
		if($trang==1) $batdau = 0;
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1, sanpham.noidung, sanpham.gia 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE loai.tenUrl = ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu 
		LIMIT ".$batdau.",9",array($tenL));
		return $query->result();
	}
	public function getSP($tenUrl)
	{
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.noidung , sanpham.gia, sanpham.hinh1, sanpham.hinh2, sanpham.hinh3, sanpham.hinh4 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE sanpham.tenUrl = ? 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu
		LIMIT 0,1",array($tenUrl));
		return $query->result();
	}
	public function getNoiBat()
	{
		$query = $this->db->query("SELECT sanpham.id, sanpham.ten, sanpham.tenUrl, sanpham.hinh1 
		FROM sanpham, loai, loaiphong, chungloai 
		WHERE sanpham.noibat = 1 
		AND sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id 
		AND sanpham.anhien = 1 AND loaiphong.anhien = 1 AND loai.anhien = 1 AND chungloai.anhien = 1 
		ORDER BY sanpham.thutu");
		return $query->result();
	}
	public function image($id,$filename,$hinh)
	{
		if($hinh==1)
		{
			$query = $this->db->query("SELECT hinh1 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh1;
			if(is_file($link))
			{
				unlink($link);
			}
			$this->db->query("UPDATE sanpham SET hinh1 = ? WHERE id = ?", array($filename, $id));
			$chk = $this->db->query("SELECT id FROM sanpham WHERE hinh1 = ? AND id = ?", array($filename, $id));
			if($chk->num_rows() > 0) return TRUE;
		}
		elseif($hinh==2)
		{
			$query = $this->db->query("SELECT hinh2 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh2;
			if(is_file($link))
			{
				unlink($link);
			}
			$this->db->query("UPDATE sanpham SET hinh2 = ? WHERE id = ?", array($filename, $id));
			$chk = $this->db->query("SELECT id FROM sanpham WHERE hinh2 = ? AND id = ?", array($filename, $id));
			if($chk->num_rows() > 0) return TRUE;
		}
		elseif($hinh==3)
		{
			$query = $this->db->query("SELECT hinh3 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh3;
			if(is_file($link))
			{
				unlink($link);
			}
			$this->db->query("UPDATE sanpham SET hinh3 = ? WHERE id = ?", array($filename, $id));
			$chk = $this->db->query("SELECT id FROM sanpham WHERE hinh3 = ? AND id = ?", array($filename, $id));
			if($chk->num_rows() > 0) return TRUE;
		}
		elseif($hinh==4)
		{
			$query = $this->db->query("SELECT hinh4 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh4;
			if(is_file($link))
			{
				unlink($link);
			}
			$this->db->query("UPDATE sanpham SET hinh4 = ? WHERE id = ?", array($filename, $id));
			$chk = $this->db->query("SELECT id FROM sanpham WHERE hinh4 = ? AND id = ?", array($filename, $id));
			if($chk->num_rows() > 0) return TRUE;
		}
		return FALSE;
	}
	public function select()
	{
		$query = $this->db->query("SELECT sanpham.id, chungloai.ten AS tenCL, loaiphong.ten AS tenLP, loai.ten AS tenL, sanpham.ten, sanpham.gia, sanpham.noibat, sanpham.thutu, sanpham.anhien FROM loaiphong, chungloai, loai, sanpham WHERE sanpham.idL = loai.id AND sanpham.idLP = loaiphong.id AND sanpham.idCL = chungloai.id ORDER BY sanpham.thutu");
		return $query->result();
	}
	public function edit($id)
	{
		$query = $this->db->query("SELECT * FROM sanpham WHERE id = ?",array($id));
		return $query->result();
	}
	public function insert($idCL, $idLP, $idL, $noibat, $ten, $gia, $noidung, $thutu)
	{
		$tenUrl = $this->unicode->change($ten);
		$noidung = $this->unicode->unicode2database($noidung);
		if(is_numeric($thutu)) settype($thutu,"int"); else $thutu = 0;
		if(is_numeric($noibat)) settype($noibat,"int"); else $noibat = 0;
		if($noibat!=1) $noibat=0;
		if(is_numeric($idCL)&&is_numeric($idLP)&&is_numeric($idL))
		{
			$this->db->query("INSERT INTO sanpham VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)", array($idCL, $idLP, $idL, $noibat, $ten, $tenUrl, $gia, NULL, NULL, NULL, NULL, $noidung, $thutu));
			$tmp = $this->db->insert_id();
			if($tmp>0)
			{
				$this->load->library('files');
				$this->files->createfolder("./upload/sanpham/".$tmp."/");
			}
			return $tmp;
		} else return 0;
	}
	public function update($id, $idCL, $idLP, $idL, $noibat, $ten, $gia, $noidung, $thutu, $anhien)
	{
		$tenUrl = $this->unicode->change($ten);
		$noidung = $this->unicode->unicode2database($noidung);
		if(is_numeric($id)&&is_numeric($idCL)&&is_numeric($idLP)&&is_numeric($idL))
		{
			if(is_numeric($thutu)) settype($thutu,"int"); else $thutu = 0;
			if(is_numeric($noibat)) settype($noibat,"int"); else $noibat = 0;
			if($noibat!=1) $noibat=0;
			$query = $this->db->query("UPDATE sanpham SET idCL = ?, idLP = ?, idL = ?, noibat = ?, ten = ?, tenUrl = ?, gia = ?, noidung =?, thutu = ?, anhien = ? WHERE id = ?", array($idCL, $idLP, $idL, $noibat, $ten, $tenUrl, $gia, $noidung, $thutu, $anhien, $id));
			$chk = $this->db->query("SELECT id FROM sanpham WHERE idCL = ? AND idLP = ? AND idL = ? AND noibat = ? AND ten = ? AND tenUrl = ? AND gia = ? AND noidung = ? AND thutu = ? AND anhien = ? AND id = ?", array($idCL, $idLP, $idL, $noibat, $ten, $tenUrl, $gia, $noidung, $thutu, $anhien, $id));
			if($chk->num_rows() > 0) return TRUE;
		} return FALSE;
	}
	public function deleteImage($id,$thutu)
	{
		settype($id,"int");
		settype($thutu,"int");
		$this->load->library('files');
		$link = NULL;
		if($thutu==1)
		{
			$query = $this->db->query("SELECT hinh1 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh1;
			$this->db->query("UPDATE sanpham SET hinh1 = NULL WHERE id = ?",array($id));
		}
		if($thutu==2)
		{
			$query = $this->db->query("SELECT hinh2 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh2;
			$this->db->query("UPDATE sanpham SET hinh2 = NULL WHERE id = ?",array($id));
		}
		if($thutu==3)
		{
			$query = $this->db->query("SELECT hinh3 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh3;
			$this->db->query("UPDATE sanpham SET hinh3 = NULL WHERE id = ?",array($id));
		}
		if($thutu==4)
		{
			$query = $this->db->query("SELECT hinh4 FROM sanpham WHERE id = ?",array($id));
			$link = './upload/sanpham/'.$id.'/'.$query->row()->hinh4;
			$this->db->query("UPDATE sanpham SET hinh4 = NULL WHERE id = ?",array($id));
		}
		if($link!=NULL)
		{
			if(is_file($link)) unlink($link);
			return TRUE;
		}
		else return FALSE;
	}
	public function delete($id)
	{
		settype($id,"int");
		$this->load->library('files');
		$query = $this->db->query("SELECT hinh1, hinh2, hinh3, hinh4 FROM sanpham WHERE id = ?",array($id));
		$link1 = './upload/sanpham/'.$id.'/'.$query->row()->hinh1;
		$link2 = './upload/sanpham/'.$id.'/'.$query->row()->hinh2;
		$link3 = './upload/sanpham/'.$id.'/'.$query->row()->hinh3;
		$link4 = './upload/sanpham/'.$id.'/'.$query->row()->hinh4;
		if(is_file($link1)) unlink($link1);
		if(is_file($link2)) unlink($link2);
		if(is_file($link3)) unlink($link3);
		if(is_file($link4)) unlink($link4);
		$this->db->query("DELETE FROM sanpham WHERE id = ?",array($id));
		$query = $this->db->query("SELECT id FROM sanpham WHERE id = ?",array($id));
		$dir = './upload/sanpham/'.$id.'/';
		$this->files->deletefolder($dir);
		if($query->num_rows()>0) return FALSE; return TRUE;
	}
}